package es.ats.codington.festival.junit.daos;


import static org.junit.Assert.*;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.daos.EventSignUpDAOImp;
import es.ats.codington.festival.junit.utils.ScriptRunner;
import es.ats.codington.festival.services.EventSignUpService;


import junit.framework.TestCase;

public class TEventSignUpDAO{

	private EventSignUpDAOImp esd;
	private EventSignUp esu;
	private static EventSignUpService ess;
	private Event ev;

	 @BeforeClass
	   public static void onceExecutedBeforeAll() {
	    	ess=new EventSignUpService();
			ScriptRunner sr=new ScriptRunner(ess.getEsd().getCon().connect(),true,true);
			try {
				FileReader fr=new FileReader("sql/newcodingtonCDTdb.sql");
				sr.runScript(fr);
			} catch (Exception e) {
				e.printStackTrace();
			}
			ess.getEsd().getCon().disconnect();
	        System.out.println("@BeforeClass: onceExecutedBeforeAll");
	    }
	
	
	
	
	@Before 
    public void setUp() throws Exception {  
       
        
        this.esd=new EventSignUpDAOImp();
        this.ess=new EventSignUpService();
        this.esu=new EventSignUp(2,3);
    	//24 Dec 2015 21:30:00 GMT
		Date d= new Date(1450992600);
		//placeId, placeName, capacity, placeDescription, workingHours
		Place p=new Place(1,"Name of the place", 1000,"Description of the place",555);
		//name, place, eventId, description, type, price, duration, seatsAvailable, start
		this.ev=new Event("Name of the event", p, 5, "Description of the event",
				"Type of the event", 999.95F, 300, 1000, d);
		
        
	}
	
	
        
   	 @After  
   	    public void tearDown() throws Exception {  
   		
   	    }
   	 
   	 
	  @Test
	    public void testRegisterVisitor() throws Exception{
		  
		  int a; 
		  a=esd.registerVisitorForNewEventDAO(esu);
		  
		  try{
			  
			  assertEquals(esu.getIdEvent(),a);
		  }
		  catch(Exception ex){
			   fail("Not yet implemented");
		  }
		  
	  }
	  
	  
	  @Test
	   public void testIsRegistered() throws Exception{
		     
		 boolean aux;
		 
		 aux=esd.isRegisteredToEvent(esu);
		 
	  try{
			  
			  assertEquals(true,aux);
		  }
		  catch(Exception ex){
			   fail("Not yet implemented");
		  }
		  
	  }
	  
	  
	  
		@Test
		   public void testUnregisterVisitor() throws Exception{
			
			
			 int aux;
			 
	
			 aux=esd.unregisterVisitorForEventDAO(esu);
			 
		  try{
				  
				  assertEquals(esu.getIdEvent(),aux);
			  }
			  catch(Exception ex){
				   fail("Not yet implemented");
			  }
			
			
		   }
		
		
		
		@Test
		   public void testviewEvents() throws Exception{
			
			 EventSignUp event=new EventSignUp(2,3);
			 
			 List<Integer> list= new ArrayList<Integer>();
			
			 
			 for(int i=0;i<list.size();i++){
				 
				 list.add(event.getIdEvent());
			 }
			 
			 EventSignUpDAOImp ev=new EventSignUpDAOImp();
			 List<Event> p = ev.viewEventsOfUserDAO(event);
			 
		  try{
				  
				  assertEquals(list,p);
			  }
			  catch(Exception ex){
				   fail("Not yet implemented");
			  }
					
	}
		
		
		
		
		@Test
		   public void testUpdateRestSeats() throws Exception{
			
				 
			  int aux; 
			  
			  aux=esd.updateRestSeatsAvailableEvent(ev);
			 
			  try{
				  
				  assertEquals(ev.getSeatsAvailable(),aux);
			  }
			  catch(Exception ex){
				   fail("Not yet implemented");
			  }
			
		   }
   	
	
	
	
	
	
	
	
	
}
